<?php

require_once('admincontroller.php');

# Back-office login controller
class Login extends AdminController
{
	function Login()
	{
		parent::AdminController(true);
		$this->load->helper('cookie');
		$this->data = array();

		// Noms des browsers pour donner des infos de compatibilité à l'utilisateur
		$this->data['navs'] = array('firefox', 'chrome', 'safari', 'ie', 'opera');
	}

	function index()
	{
		if ($this->session->userdata('admin_connect')) {
			redirect('/admin/'.DEFAULT_ADMIN_CTRL);
		}
		$this->data['email'] = '';
		$this->data['remember_me'] = 0;
		$this->load->view('admin/admin_login', $this->data);
	}

	function connection()
	{
		$email = $this->input->post('email');
		$password = crypt_password($this->input->post('password'));

		if ($this->connect($email, $password)) {
			// cookie management
			if ($this->input->post('remember_me')) {
				set_cookie('login', $email, '31536000');
				set_cookie('password', $password, '31536000');
			}
			else {
				delete_cookie('login');
				delete_cookie('password');
			}
			redirect('/admin/'.DEFAULT_ADMIN_CTRL);
		}
		else {
			// re-display login form
			$this->data['error_notice'] = 'Erreur d\'authentification';
			$this->data['email'] = $email;
			$this->data['remember_me'] = $this->input->post('remember_me');
			$this->load->view('admin/admin_login', $this->data);
		}
	}
}
